package com.woniu.service.idiom.impl;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.woniu.entity.Nuser;
import com.woniu.entity.idiom.IdiomInviteRecEntity;
import com.woniu.entity.idiom.IdiomRedBeansRecEntity;
import com.woniu.mapper.idiom.IdiomRedBeansRecMapper;
import com.woniu.service.NuserService;
import com.woniu.service.idiom.IdiomRedBeansRecService;
import com.woniu.utils.TokenUtil;
import com.woniu.vo.idiom.RedBeansRecResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;





@Service("idiomRedBeansRecService")
public class IdiomRedBeansRecServiceImpl extends ServiceImpl<IdiomRedBeansRecMapper, IdiomRedBeansRecEntity> implements IdiomRedBeansRecService {


    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private NuserService nuserService;

    @Override
    public List<RedBeansRecResp> redBeansRec() {
        //获取用户
        //String userId = tokenUtil.getUserId();
        String userId = "test";
        Nuser nuser = nuserService.getById(userId);
        if (nuser == null) {
            return null;
        }
        // 根据时间倒序排列
        List<IdiomRedBeansRecEntity> list = baseMapper.selectList(new QueryWrapper<IdiomRedBeansRecEntity>()
                .eq("uid", userId)
                .gt("beans_count", 0)
                .orderByDesc("create_time")
        );
        List<RedBeansRecResp> result = Lists.newArrayList();
        for(IdiomRedBeansRecEntity entity: list){
            RedBeansRecResp resp = new RedBeansRecResp();
            resp.setTime(DateUtil.format(entity.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            resp.setCount(entity.getBeansCount());
            resp.setDesc(entity.getType());
            result.add(resp);
        }
        return result;
    }

    @Override
    public List<RedBeansRecResp> withdrawRec() {
        //获取用户
        //String userId = tokenUtil.getUserId();
        String userId = "test";
        Nuser nuser = nuserService.getById(userId);
        if (nuser == null) {
            return null;
        }
        // 根据时间倒序排列
        List<IdiomRedBeansRecEntity> list = baseMapper.selectList(new QueryWrapper<IdiomRedBeansRecEntity>()
                .eq("uid", userId)
                .lt("beans_count", 0)
                .orderByDesc("create_time")
        );
        List<RedBeansRecResp> result = Lists.newArrayList();
        for(IdiomRedBeansRecEntity entity: list){
            RedBeansRecResp resp = new RedBeansRecResp();
            resp.setTime(DateUtil.format(entity.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            resp.setCount(entity.getBeansCount());
            resp.setDesc(entity.getType());
            result.add(resp);
        }
        return result;
    }
}